
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
 * ItemList.java
 *
 * Created on Nov 18, 2013, 4:04:31 PM
 */
package openmoneyspinnersuite;

/**
 *
 * @author Abiodun
 */
public class ItemList extends javax.swing.JDialog {
    Systems systems=new Systems();
    Thread processThread=new Thread();
    Thread titleThread=new Thread();
    String[] dbItem;
    /** Creates new form ItemList */
    public ItemList(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        /* Execute refresh thread */
        processThread=new Thread(new Runnable() {
            public void run() {
                disableControls();
                refreshList("");
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("accountThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jSelectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMSApp.class).getContext().getResourceMap(ItemList.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setName("Form"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jSelectButton.setText(resourceMap.getString("jSelectButton.text")); // NOI18N
        jSelectButton.setName("jSelectButton"); // NOI18N
        jSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSelectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSelectButton)
                .addGap(6, 6, 6))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSelectButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jSelectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSelectButtonActionPerformed
    dispose();
}//GEN-LAST:event_jSelectButtonActionPerformed

private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
    if(evt.getKeyChar()=='\n')
    {
        System.out.println("key char= "+evt.getKeyChar());
        /* Execute refresh thread */
        processThread=new Thread(new Runnable() {
            public void run() {
                disableControls();
                if(jTextField1.getText()!=null)
                refreshList(jTextField1.getText());
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("accountThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
    }
}//GEN-LAST:event_jTextField1KeyTyped
                              
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. Item List - Processing Request");
            thread.sleep(500);
            this.setTitle("::. Item List - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. Item List - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. Item List - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
    void disableControls(){
        jTextField1.setEnabled(false);
        jSelectButton.setEnabled(false);
    }
    void enableControls(){
        jTextField1.setEnabled(true);
        jSelectButton.setEnabled(true);
        this.setTitle("::. Item List");
    }
    void refreshList(String searchPara)
    {        
        if(searchPara.isEmpty()==false)
        {
            String multipleSearch[]=searchPara.split(",");
            String queryPara="where (cItemCode = '"+multipleSearch[0]+"') or (vName = '"+multipleSearch[0]+"') or (cType = '"+multipleSearch[0]+"') ";

            for(int k=1;k<multipleSearch.length;k++)
            {
                queryPara+="or (cItemCode like '%"+multipleSearch[k]+"%') or (vName like '%"+multipleSearch[k]+"%') or (cType like '%"+multipleSearch[k]+"%') ";
            }

            dbItem=systems.getColumn(OpenMSApp.Database_A, "Select vName from Item "+queryPara,1);
        }
        else
        dbItem=systems.getColumn(OpenMSApp.Database_A, "Select vName from Item ",1);
        resetItem();
    }
public String getSelectedItem()
{
    return String.valueOf(jList1.getSelectedValue());
}

    public void resetItem()
    {
        
        jList1.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return dbItem.length; }
            public Object getElementAt(int i) { return dbItem[i]; }
        });
         //jList1=new javax.swing.JList(dbItem);jList1.setListData(dbItem);
        
        //dbItem=null;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jSelectButton;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
